package service

import (
	"aiRole/internal/dao/model"
	"aiRole/internal/dao/query"
	result "aiRole/internal/dto/reslult"
	"aiRole/internal/dto/response"
	initSystem "aiRole/pkg/init"
	"github.com/gin-gonic/gin"
)

type MessageService struct {
}

func (s MessageService) Page(c *gin.Context, page int, size int, id int) {
	var rstPage response.PageResponse
	messageDB := query.Use(initSystem.DB).ConversationMessage
	var pageList []*model.ConversationMessage
	count, err := messageDB.WithContext(c).Where(messageDB.ConversationID.Eq(int64(id))).ScanByPage(&pageList, page, size)
	if err != nil {
		initSystem.Logger.Error(err.Error())
		result.Error(c, 500, "服务器错误")
		return
	}
	rstPage.List = pageList
	rstPage.Total = count
	page = page + 1
	rstPage.Page = page
}

func (s MessageService) List(context *gin.Context, id int) {
	messageDB := query.Use(initSystem.DB).ConversationMessage
	list, err := messageDB.WithContext(context).Where(messageDB.ConversationID.Eq(int64(id))).Order(messageDB.CreatedAt.Desc()).Find()
	if err != nil {
		initSystem.Logger.Error(err.Error())
		result.Error(context, 500, "服务器错误")
		return
	}
	result.Ok(context, list)
}
